// pages/myset/setpaymentcode/index.js
const app = getApp() // 引入app
import _request from '../../../utils/request.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl, //全局img路径
    oldpassword: '',
    newpassword: '',
    newpasswordcoyp: '',
    active: 1,
    btnvisible: true,
    count_down: 60,
    code: '',
    cardcode: '',
    bool:false,
  },

  // 获取密码
  getpassword(e) {
    this.setData({
      oldpassword: e.detail.inputValue
    })
  },

  // 获取新密码
  getnewpassword(e) {
    this.setData({
      newpassword: e.detail.inputValue
    })
  },

  // 获取新密码
  getnewpasswordcoyp(e) {
    this.setData({
      newpasswordcoyp: e.detail.inputValue
    })
  },

  // 获取验证码
  getCode() {
    _request.postObj('/userSms/getPhoneCode', {
      mobile: this.data.phone,
      userId: wx.getStorageSync('userid'),
      type: 2,
    }).then((res) => {
      if (res.data.code === 200) {
        this.get_code_settime()
      }
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  // 下一步
  toNext() {
    const {
      oldpassword
    } = this.data
    // 验证密码
    this.setData({
      bool : true,
      value: ''
    })
    _request.postObj('/settings/checkPayWord', {
      userId: wx.getStorageSync('userid'),
      payWord: oldpassword
    }).then((res) => {
      if (res.data.code === 200) {
        this.setData({
          active: 2
        })
      }
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  // 忘记密码
  onAction() {
    wx.setNavigationBarTitle({
      title: '身份验证'
    })
    this.setData({
      active: 3
    })
  },

  // 确认
  toConfirm() {
    const {
      newpasswordcoyp,
      newpassword
    } = this.data

    // 修改密码
    _request.postObj('/settings/setPaypassword', {
      userId: wx.getStorageSync('userid'),
      copyPayword: newpasswordcoyp,
      payword: newpassword
    }).then((res) => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '修改密码成功',
          icon: 'none',
          duration: 1000
        })
        setTimeout(() => {
          wx.redirectTo({
            url: '/pages/myset/index',
          })
        }, 1000);
      }
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  // 手机号码验证
  toNextCode() {
    const { phone,code } = this.data
    if(phone.length < 11 || !phone){
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none',
        duration: 1000
      })
    }else if(!code){
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 1000
      })
    }else{
      _request.postObj('/userSms/phoneCodeCheck', {
        userId: wx.getStorageSync('userid'),
        mobile: this.data.phone,
        type: 2,
        phoneCode: this.data.code
      }).then((res) => {
        if (res.data.code === 200) {
          wx.setNavigationBarTitle({
            title: '设置支付密码'
          })
          this.setData({
            active: 2
          })
        }
      }).catch((error) => {
        console.log(error, 'error');
      })
    }
    
  },

  //验证码按钮倒计时方法
  get_code_settime(that) {
    var that = this;
    let countDown = that.data.count_down; //获取倒计时初始值
    that.setData({
      //这里把setInterval复制给变量名timer
      timer: setInterval(function () {
        // 每隔一秒减1
        countDown--;
        that.setData({
          btnvisible: false, //此处用于隐藏验证码点击按钮，使倒计时显示
          count_down: countDown //倒计时计数 
        })
        //当计数为0时，清空定时器，使验证码按钮再次显示
        if (countDown == 0) {
          clearInterval(that.data.timer); //清除timer，
          that.setData({
            btnvisible: true,
            count_down: '60'
          })
        }
      }, 1000)
    })
  },

  // 身份证
  bindCardInput() {
    this.setData({
      cardcode: e.detail.value
    })
  },

  // 手机号
  bindPhoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  // 验证码
  bindCodeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})